package Function_Study;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class FunctionDemo4 {
    public static void main(String[] args) {
        /*
            引用构造方法
            格式:
                类名 ::new
            示例:
                Student::new
            目的:
                创建这个类的对象
            要求:
                集合里面存储姓名和年龄,要求封装成Actor对象并收集到List集合中
          方法引用
          1.引用处需要是函数式接口
          2.被引用的方法需要已经存在
          3.被引用的方法的形参和返回值需要跟抽象方法的形参和返回值保持一致
          4.被引用的方法功能需要满足当前需求

         */
        ArrayList<String> actress = new ArrayList<>();
        Collections.addAll(actress, "唐嫣,22", "杨洋,23", "杨紫,23", "杨幂,34", "赵丽颖,21", "迪丽热巴,25");

        /*List<Actor> newList = actress.stream().map(new Function<String, Actor>() {
            @Override
            public Actor apply(String s) {
                return new Actor(s.split(",")[0],
                        Integer.parseInt(s.split(",")[1]));
            }
        }).collect(Collectors.toList());
        System.out.println(newList);*/

        List<Actor> newList = actress.stream().map(Actor::new).toList();
        System.out.println(newList);
    }
}
class Actor{
    private String name;
    private int age;

    public Actor() {
    }

    public Actor(String s) {
        this.name = s.split(",")[0];
        this.age = Integer.parseInt(s.split(",")[1]);
    }

    public Actor(String name, int age) {
        this.name = name;
        this.age = age;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    public String toString() {
        return "[" + name + ", " + age + "]";
    }
}

